﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.Model.Repositories.Abstract;
using BusinessLayer.Model.Entities;
using System.Data.Linq;

namespace DataAccess.Repositories
{
    public class AccountRepository : IAccountRepository
    {
        private readonly IntecDataContext _db = new IntecDataContext();
        private readonly Table<Account> _accountsTable;

        public AccountRepository()
        {
            _accountsTable = _db.GetTable<Account>();
        }

        #region IAccountRepository Members

        public Account FetchByUsername(string username)
        {
            return (from a in _accountsTable
                    where a.Username == username
                    select a).First();
        }

        public void Add(Account account)
        {
            _accountsTable.InsertOnSubmit(account);
        }

        public void ChangePassword(Account account, string newpass)
        {
            account.Password = newpass;
            SubmitChanges();
        }

        public void UpdateOnSubmit(Account account)
        {
            return;
        }

        public void SubmitChanges()
        {
            _db.SubmitChanges();
        }

        public bool AccountExists()
        { 
            return (_accountsTable.AsEnumerable<Account>().Count() == 1) ? true : false;
        }

        #endregion
    }
}
